package com.example.video.common.security;

import com.example.video.admin_server.entity.SysRole;
import com.example.video.admin_server.entity.SysUser;
import com.example.video.common.Constant;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author : yj
 */
public class SysUserDetails implements UserDetails {

    private SysUser sysUser;

    private List<SysRole> roles;

    public SysUserDetails(SysUser sysUser, List<SysRole> roles){
        this.sysUser = sysUser;
        this.roles = roles;
    }

    public SysUser getSysUser(){
        return sysUser;
    }

    public List<SysRole> getRoles(){
        return roles;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        for (SysRole role : roles){
            authorities.add(new SimpleGrantedAuthority(role.getSign()));
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return sysUser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        boolean equals = Constant.Status.ENABLE.value.equals(sysUser.getStatus());
        return equals;
    }
}
